﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.WindowsMobile.PocketOutlook;

namespace Cryptophone2
{
    public partial class ContactList_ : Form
    {
        List<String> _items = new List<String>();
        List<String> _itemsIndexNumber = new List<String>();
        String numberSelected;
        
        public ContactList_()
        {
            InitializeComponent();

            OutlookSession mySession = new OutlookSession();
            ContactCollection collection = mySession.Contacts.Items;

            // Scroll all contacts
            foreach (Contact contact in collection)
            {
                //Add all contacts to a list
                String Name = "        " + contact.FirstName + " " + contact.LastName;
                _itemsIndexNumber.Add(contact.MobileTelephoneNumber);
                _items.Add(Name);
                
            }

            // Set datasource of listbox with contacts list
            listBox_contacts.DataSource = _items;
            
        }

        /// <summary>
        /// Click Return button on Menu
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuItemr_return_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        /// <summary>
        /// Click Call button on Menu. It sets the contact selected
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuItem_call_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Yes;

            int index = listBox_contacts.SelectedIndex;
            numberSelected = _itemsIndexNumber.ElementAt(index);

        }

        /// <summary>
        /// Used to retrive the selected items
        /// </summary>
        /// <returns>Selected contact items</returns>
        public String GetNumber()
        {
            return numberSelected;
        }
    }
}